using System;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;

namespace OurWedding1.Models
{ 
    public class ConfirmationKeyRepository : IConfirmationKeyRepository
    {
        readonly WeddingEntities _context = new WeddingEntities();

        public IQueryable<ConfirmationKey> All
        {
            get { return _context.ConfirmationKeys; }
        }

        public IQueryable<ConfirmationKey> AllIncluding(params Expression<Func<ConfirmationKey, object>>[] includeProperties)
        {
            return includeProperties.Aggregate<Expression<Func<ConfirmationKey, object>>, IQueryable<ConfirmationKey>>(_context.ConfirmationKeys, (current, includeProperty) => current.Include(includeProperty));
        }

        public ConfirmationKey Find(int id)
        {
            return _context.ConfirmationKeys.Find(id);
        }

        public void InsertOrUpdate(ConfirmationKey confirmationkey)
        {
            if (confirmationkey.ConfirmationKeyId == default(int)) {
                // New entity
                _context.ConfirmationKeys.Add(confirmationkey);
            } else {
                // Existing entity
                _context.Entry(confirmationkey).State = EntityState.Modified;
            }
        }

        public void Delete(int id)
        {
            var confirmationkey = _context.ConfirmationKeys.Find(id);
            _context.ConfirmationKeys.Remove(confirmationkey);
        }

        public void Save()
        {
            _context.SaveChanges();
        }

        public void Dispose() 
        {
            _context.Dispose();
        }
    }

    public interface IConfirmationKeyRepository : IDisposable
    {
        IQueryable<ConfirmationKey> All { get; }
        IQueryable<ConfirmationKey> AllIncluding(params Expression<Func<ConfirmationKey, object>>[] includeProperties);
        ConfirmationKey Find(int id);
        void InsertOrUpdate(ConfirmationKey confirmationkey);
        void Delete(int id);
        void Save();
    }
}